package com.qf.hospital.system.controller;

import com.github.pagehelper.PageInfo;
import com.qf.hospital.system.entity.Cashier;
import com.qf.hospital.system.entity.Pharmacy;
import com.qf.hospital.system.service.CashierService;
import com.qf.hospital.system.service.PharmacyService;
import com.qf.hospital.system.service.ReportService;
import com.qf.hospital.system.util.R;
import com.qf.hospital.system.vo.ReportVO;
import com.qf.hospital.system.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CashierController {

    @Autowired
    private ReportService reportService;

    @Autowired
    private PharmacyService pharmacyService;

    @Autowired
    private CashierService cashierService;


    /**
     * 处方划价的查询患者
     * # path & method
     * Request URL: http://localhost:8080/caotake/tselpreson?page=1&limit=5
     * Request Method: GET
     * # parameter
     * name:  String 查询患者的条件
     * page:	...
     * limit: 	..
     * # return
     * {
     * "code": 0,
     * "msg": "",
     * "count": 12
     * "data": [
     * {....}
     * ]
     * }
     */
    @GetMapping("/caotake/tselpreson")
    public ResultVO selectPerson(String name, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer limit) {
        //1. service
        PageInfo<ReportVO> pageInfo = reportService.findByConditionAndPage(page, limit, name);
        //2. return
        return R.ok(pageInfo.getTotal(), pageInfo.getList());
    }


    /**
     * 查询全部药品信息
     * # path & method
     * Request URL: http://localhost:8080/caocc/seldrug?page=1&limit=5
     * Request Method: GET
     * # parameter
     * durgname: 1234
     * page: 1
     * limit: 5
     * # return
     * {
     * "code": 0,
     * "msg": "",
     * "count": 12
     * "data": [
     * {"pharmacyName": "甘草片","drugstoreNum": 58,"unitName": "盒","sellingPrice": 50},
     * ……
     * ]
     * }
     */
    @GetMapping("/caocc/seldrug")
    public ResultVO selectDrug(String durgname, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer limit) {
        //1. service
        PageInfo<Pharmacy> pageInfo = pharmacyService.findByPharmacyName(durgname, page, limit);

        //2. return
        return R.ok(pageInfo.getTotal(), pageInfo.getList());
    }


    /**
     * 查询指定患者的病因
     * # path & method
     * Request URL: http://localhost:8080/caocc/selbing
     * Request Method: POST
     * # parameter
     * reid: 33
     * # return
     * 没有病因： 返回空字符串
     * 有病因：正常返回病因字符串
     */
    @PostMapping(value = "/caocc/selbing", produces = "text/html;charset=UTF-8")
    // application/json;charset=utf-8      text/plain  ISO8859-1
    public String selectBind(Integer reid) {
        return reportService.findPathogenyByReportId(reid);
    }


    /**
     * 修改病因
     * # path & method
     * Request URL: http://localhost:8080/caocc/addbing
     * Request Method: POST
     * # parameter
     * reid: 33
     * bing: 斗鸡眼治好了.针眼犯了
     * # return
     * SUCCESS:
     * {
     * "code": 0,
     * "msg": ""
     * }
     */
    @PostMapping("/caocc/addbing")
    public ResultVO addBing(String bing, Integer reid) {
        //1. service
        reportService.updatePatientPathogenyByReportId(bing, reid);
        //2. return
        return R.ok();
    }

    /**
     * 查询数据条数,根据患者id和药品名称
     * # path & method
     * Request URL: http://localhost:8080/caocc/selchu
     * Request Method: POST
     * # parameters
     * reid: 33
     * mename: 川贝
     * # return
     * 数值类型
     */
    @PostMapping("/caocc/selchu")
    public Integer selectChu(String mename, Integer reid) {
        //1. 歪比巴卜
        return cashierService.findCountByReportIdAndDrugName(reid, mename);
    }

    /**
     * 添加
     * # path & method
     * Request URL: http://localhost:8080/caocc/addchu
     * Request Method: POST
     * # parameters
     * registerId: 33
     * name: 枇杷
     * num: 1
     * price: 30
     * totalPrice: 30
     * drugstorenum: 1
     * # return
     * 无所谓
     */
    @PostMapping("/caocc/addchu")
    public void addChu(Cashier cashier) {
        //1. s
        cashierService.insert(cashier);
    }


    /**
     * 修改药品购买信息
     * # path & method
     * Request URL: http://localhost:8080/caocc/updchu
     * Request Method: POST
     * # parameters
     * registerId: 43
     * name: 枇杷
     * num: 2
     * totalPrice: 60
     * drugstorenum: 2
     * # return
     * 无所谓
     */
    @PostMapping(value = "/caocc/updchu")
    public void updateChu(Cashier cashier) {
        //1. s
        cashierService.update(cashier);
    }


    /**
     * 查!药!
     * # path & method
     * Request URL: http://localhost:8080/caocc/selAll?perid=33&page=1&limit=5
     * Request Method: GET
     * # parameter
     * perid: 33  (reportId)
     * page: 1
     * limit: 5
     * # return
     * {
     * "code": 0,
     * "msg": "",
     * "count": 11,
     * "data": [
     * {"name": "琵琶","num": 4,"price": 30,"totalPrice": 120,"state": 1},
     * .....
     * ]
     * }
     */
    @GetMapping("/caocc/selAll")
    public ResultVO selectAll(Integer perid, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer limit) {
        // s
        PageInfo<Cashier> pageInfo = cashierService.findByReportAndByPage(page, limit, perid);
        // r
        return R.ok(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 删！药！
     * # path & method
     *     Request URL: http://localhost:8080/caocc/del
     *     Request Method: POST
     * # parameter
     * 	id: 46
     * # return
     * {
     *     "code": 0
     *     "msg": ""
     * }
     */
    @PostMapping("/caocc/del")
    public ResultVO del(Integer id){
        // s
        cashierService.deleteCashierById(id);
        // r
        return R.ok();
    }

}
